const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class WeChatMessageEndpointHandler extends BaseHandler {

    get pattern() {
        return "/wechat/:app_id/endpoint";
    }

    doPOST(request, response) {
        request.query.app_id = request.params.app_id;
        response.setHeader('Content-Type', request.headers['content-type']);
        request.query.host = request.headers.host;
        this.Controller.handleWeChatUserMessage(request.query, request.body, (data) => {
            if (request.session) {
                request.session.destroy((error) => {
                    response.end(data);
                });
            } else {
                response.end(data);
            }
        });

        //response.end('success');
    }

    doGET(request, response) {
        response.end('success');
    }

    setup() {
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = WeChatMessageEndpointHandler;